package com.bw.z.network.interceptor;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.bw.z.utils.SpUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 针对接口中token限定的拦截器
 * 像每个接口中的请求 头添加token
 */
public class TokenSignInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //先获取原有Request
        Request oldRequest = chain.request();
        //配置添加过token的Request
        Request.Builder builder = new Request.Builder();
        builder.url(oldRequest.url());
        builder.headers(oldRequest.headers());
        String token = SpUtils.readData("token");
        LogUtils.e("token:" + token);
        builder.header("token", token);
        builder.post(oldRequest.body());
        Request request = builder.build();
        return chain.proceed(request);
    }
}
